import unittest,gc

from PyOEUO import stack

class TestStack(unittest.TestCase):
    def setUp(self):
        self.st=stack.Stack()

    def tearDown(self):
        self.st.Close()

    def testStackOps(self):
        self.failUnlessEqual(len(self.st),0)

        vals=[1,2,'foo','bar',True,False]

        for x in vals:
            self.st.Push(x)

        self.failUnlessEqual(len(self.st),6)
        self.failUnlessEqual(repr(list(self.st)),repr(vals))
        self.failUnlessRaises(IndexError,self.st.GetValue,10)
        self.failUnlessRaises(TypeError,self.st.GetValue,1,object)
        self.failUnlessRaises(IndexError,self.st.GetValue,-20)

        self.st.SetTop(0)

        self.failUnlessEqual(len(self.st),0)
        self.failUnlessEqual(list(self.st),[])

        self.st.Push(42.12)
        self.failUnlessEqual(len(self.st),1)
        self.failUnlessEqual(list(self.st),[42])
        self.failUnlessAlmostEqual(self.st.GetValue(1,float),42.12)

    def testCalls(self):
        self.failUnlessEqual(len(self.st),0)

        self.failUnless('C' in set(self.st.Query('GetFeatures')))
        self.failUnless('V' in set(self.st.Query('GetFeatures')))
        self.failUnlessEqual(self.st.Execute('GetVar','CliNr'),0)


    def testMemory(self):
        self.failUnlessEqual(len(self.st),0)

        for x in xrange(100):
            self.st.Push('%08x'%x)
            gc.collect(2)

        for x in xrange(100):
            self.failUnlessEqual(self.st[x],'%08x'%x)
            gc.collect(2)

if __name__ == '__main__':
    unittest.main()
